/* $RCSFile: createdb.sql $
 Apache license v2 2011 by Christian Surieux
 Removing this comment is forbiddden
 $Source: ?/AddonNice/Setup/Scripts/createdb.sql $
 $log$
 Revision 37 2010/08/09 12:26:56  christian.surieux
   Adding Log comment to sql files
*/
USE [Master]

IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'AddonNice')
BEGIN
	DECLARE @spid smallint
	DECLARE @sql varchar(4000)

	DECLARE crsr CURSOR FAST_FORWARD FOR
		SELECT spid FROM sysprocesses p INNER JOIN sysdatabases d ON d.[name] = 'AddonNice' AND p.dbid = d.dbid

	OPEN crsr
	FETCH NEXT FROM crsr INTO @spid

	WHILE @@FETCH_STATUS != -1
	BEGIN
		SET @sql = 'KILL ' + CAST(@spid AS varchar)
		EXEC(@sql) 
		FETCH NEXT FROM crsr INTO @spid
	END

	CLOSE crsr
	DEALLOCATE crsr

	DROP DATABASE [AddonNice]
END
GO

CREATE DATABASE [AddonNice]
GO

ALTER DATABASE [AddonNice]
COLLATE SQL_Latin1_General_CP1_CI_AS
GO

EXEC sp_dboption N'AddonNice', N'autoclose', N'false'
GO

EXEC sp_dboption N'AddonNice', N'bulkcopy', N'false'
GO

EXEC sp_dboption N'AddonNice', N'trunc. log', N'false'
GO

EXEC sp_dboption N'AddonNice', N'torn page detection', N'true'
GO

EXEC sp_dboption N'AddonNice', N'read only', N'false'
GO

EXEC sp_dboption N'AddonNice', N'dbo use', N'false'
GO

EXEC sp_dboption N'AddonNice', N'single', N'false'
GO

EXEC sp_dboption N'AddonNice', N'autoshrink', N'false'
GO

EXEC sp_dboption N'AddonNice', N'ANSI null default', N'false'
GO

EXEC sp_dboption N'AddonNice', N'recursive triggers', N'false'
GO

EXEC sp_dboption N'AddonNice', N'ANSI nulls', N'false'
GO

EXEC sp_dboption N'AddonNice', N'concat null yields null', N'false'
GO

EXEC sp_dboption N'AddonNice', N'cursor close on commit', N'false'
GO

EXEC sp_dboption N'AddonNice', N'default to local cursor', N'false'
GO

EXEC sp_dboption N'AddonNice', N'quoted identifier', N'false'
GO

EXEC sp_dboption N'AddonNice', N'ANSI warnings', N'false'
GO

EXEC sp_dboption N'AddonNice', N'auto create statistics', N'true'
GO

EXEC sp_dboption N'AddonNice', N'auto update statistics', N'true'
GO

-- GRANT ACCESS TO NETWORK_SERVICES
USE [AddonNice]

DECLARE @networkserv nvarchar(256)
SELECT @networkserv	=	SUSER_SNAME(0x010100000000000514000000)
IF NOT EXISTS (SELECT * FROM  master.dbo.syslogins WHERE name = @networkserv )
BEGIN
	DECLARE @Stmt nvarchar(4000)
	SELECT @Stmt = 'CREATE LOGIN '
	SELECT @Stmt = @Stmt + quotename(@networkserv,']')
	SELECT @Stmt = @Stmt + ' FROM WINDOWS'
	EXEC (@Stmt)
END
IF NOT EXISTS (SELECT * FROM  master.dbo.sysusers WHERE name = @networkserv )
BEGIN
SELECT @Stmt = 'CREATE USER '
SELECT @Stmt = @Stmt + quotename(@networkserv,']')
SELECT @Stmt = @Stmt + ' FOR LOGIN '
SELECT @Stmt = @Stmt + quotename(@networkserv,']')
EXEC (@Stmt)
-- EXEC sp_grantdbaccess @login
END
EXEC sp_addrolemember 'db_owner', @networkserv 
GO